//
//  NSString+Str.m
//  wzry
//
//  Created by 酷安 @啥也不会啊啊啊啊啊啊  on 2023/9/7.
//

#import "NSString+Str.h"

@implementation NSString (Str)
- (NSString *)xy_to_Datetime:(nullable NSString*)format{
    if(format == nil){
        format = @"yyyy-MM-dd HH:mm:ss";
    }
    // 将时间戳转换为NSDate
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.doubleValue];

    // 创建日期格式化器，用于将NSDate格式化为字符串
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format]; // 设置日期格式
    // 将NSDate转换为字符串
    NSString *dateString = [dateFormatter stringFromDate:date];
    return dateString;
}

- (NSString *)xy_to_minutes_seconds{
    NSInteger minutes = self.integerValue / 60;
    NSInteger seconds = self.integerValue % 60;
    return [NSString stringWithFormat:@"%ld:%2ld",minutes, seconds];
}


- (NSString *)base64{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSString *stringBase64 = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; // base64格式的字符串
    return stringBase64;
}

- (NSString *)base64String{
    NSData *data = [[NSData alloc] initWithBase64EncodedString:self options:NSDataBase64DecodingIgnoreUnknownCharacters];
            
    NSString *string =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return string;
}


- (NSString *)toHMS{
    NSInteger totalSeconds = self.integerValue;
    NSInteger hours = totalSeconds / 3600;
    NSInteger minutes = (totalSeconds % 3600) / 60;
    NSInteger seconds = totalSeconds % 60;
    return [NSString stringWithFormat:@"%02ld小时%02ld分钟%02ld秒", (long)hours, (long)minutes, seconds];
}

- (NSString *)bytesToGMK{
    NSInteger bytes = self.integerValue;
    return [NSString bytesToGMK:bytes];
}

+ (NSString *)bytesToGMK:(NSInteger)bytes{
    double TB = bytes / (1024.0 * 1024.0 * 1024.0 * 1024);
    double GB = bytes / (1024.0 * 1024.0 * 1024.0);
    double MB = bytes / (1024.0 * 1024.0);
    double KB = bytes / 1024.0;
    if (TB >= 1.0) {
        return [NSString stringWithFormat:@"%.2f TB", TB];
    } else if (GB >= 1.0) {
        return [NSString stringWithFormat:@"%.2f GB", GB];
    } else if (MB >= 1.0) {
        return [NSString stringWithFormat:@"%.2f MB", MB];
    } else {
        return [NSString stringWithFormat:@"%.2f KB", KB];
    }
}

+ (NSString *)bytesToG:(NSInteger)bytes appendunit:(BOOL)appendunit{
    double GB = bytes / (1024.0 * 1024.0 * 1024.0);
    if(appendunit){
        return [NSString stringWithFormat:@"%.2f GB", GB];
    }else{
        return [NSString stringWithFormat:@"%.2f", GB];
    }
}

+ (NSString *)xy_getCurrent_Year{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDate *now = [NSDate date];
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    return [NSNumber numberWithInteger:dateComponent.year].stringValue;
}
@end
